<?php
namespace Bootstrap;
defined("APP_VERSION") or exit("该脚本只能被包含");
//调试模式下显示错误到屏幕
if (defined("APP_DEBUG") && APP_DEBUG) {
    error_reporting(E_ALL);
    ini_set("display_errors", "1");
    ini_set("html_errors", "1");
}

//引导类
class Bootstrap
{
    private $di;
    private $config;
    private $application;

    public static function run()
    {
        $bootstrap = new Bootstrap();
        $bootstrap->start();
    }

    public function start()
    {
        //实例化di
        $this->di = new \Phalcon\Di\FactoryDefault();
        $this->initConfig();//初始化配置文件
        $this->initAutoloader();//初始化自动加载配置
        $this->initRouter();//初始化路由

        try {
            //处理请求
            $this->application = new \Phalcon\Mvc\Application();
            $this->di->setShared("application", $this->application);
            $this->application->setDi($this->di);
            if (isset($this->config->multi_modules) && $this->config->multi_modules == true) {
                //multi_modules初始化
                $this->initModules();

            } else {
                //signle_module初始化
                $this->initSingle();
            }
            echo $this->application->handle()->getContent();
        } catch (\Phalcon\Exception $e) {
            if (defined("APP_DEBUG") && APP_DEBUG) {
                echo "PhalconException: ", $e->getMessage(), " on " . $e->getFile() . " line:" . $e->getLine() . " code:" . $e->getCode();
            } else {
                echo "服务器内部错误";
            }
        }
    }

    //初始化配置
    private function initConfig()
    {
        //读取默认配置文件
        $this->config = new \Phalcon\Config(include ROOT_PATH . "/Conf/config.php");
        $this->di->setShared("config", $this->config);//将配置参数对象注册为服务
    }

    //初始化autoload
    private function initAutoloader()
    {
        //注册autoloader服务
        $loader = new \Phalcon\Loader();
        $autoloads = $this->config->autoload->toArray();//配置文件中的autoload
        $loader->registerNamespaces(array_merge(array("Root" => ROOT_PATH, "Bootstrap" => dirname(__FILE__)), $autoloads));
        $loader->register();
        $this->di->setShared("loader", $loader);
    }

    //初始化路由
    private function initRouter()
    {
        //注册路由
        $this->di->setShared("router", function () {
            $router = new \Phalcon\Mvc\Router();
            $router->setDefaultController("index");
            $router->setDefaultAction("index");

            return $router;
        });
    }

    //多模块模式
    private function initModules()
    {
        $this->di->get("router")->setDefaultModule($this->config->default_module);//为路由设置默认模块
        //为每个模块设置路由规则
        foreach ($this->config->modules as $module_name => $module) {
            $this->di->get("router")->add("/{$module_name}/?([a-zA-Z0-9_-]*)/?([a-zA-Z0-9_]*)(/.*)*", array(
                'module'     => $module_name,
                'controller' => 1,
                'action'     => 2,
                'params'     => 3
            ));
        }

        $multi_boot = new \Bootstrap\MultiBoot();//模块引导
        $multi_boot->setDi($this->di)->register();//注册模块相关服务和配置
    }

    //单模块模式
    private function initSingle()
    {
        $this->di->get("router")->setDefaultNamespace("App\Controllers\\");
        $this->di->get("router")->add("/:controller/:action/:params", array(
            'controller' => 1,
            'action'     => 2,
            'params'     => 3
        ));
        $this->di->setShared('view', function () {
            $view = new \Phalcon\Mvc\View();
            $view->setViewsDir(APP_PATH . '/Views/');

            return $view;
        });
    }
}

Bootstrap::run();//启动应用



